package com.ruoyi.performance.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.ruoyi.performance.mapper.PerfCheckreportMapper;
import com.ruoyi.performance.domain.PerfCheckreport;
import com.ruoyi.performance.domain.PerfCheckresult;
import com.ruoyi.performance.service.IPerfCheckreportService;

import lombok.extern.slf4j.Slf4j;

import com.google.common.base.Joiner;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;

/**
 * 绩效报告单Service业务层处理
 * 
 * @author ygx
 * @date 2020-09-06
 */
@Service
@Slf4j
public class PerfCheckreportServiceImpl implements IPerfCheckreportService 
{
    @Autowired
    private PerfCheckreportMapper perfCheckreportMapper;
    @Autowired
    private PerfCheckServiceImpl perfCheckServiceImpl;

    /**
     * 查询绩效报告单
     * 
     * @param perfcheckreportId 绩效报告单ID
     * @return 绩效报告单
     */
    @Override
    public PerfCheckreport selectPerfCheckreportById(Long perfcheckreportId)
    {
        return perfCheckreportMapper.selectPerfCheckreportById(perfcheckreportId);
    }

    /**
     * 查询绩效报告单列表(年度)
     * 
     * @param perfCheckreport 绩效报告单
     * @return 绩效报告单
     */
	@Override
    public List<PerfCheckreport> selectPerfCheckreportYearList(PerfCheckreport perfCheckreport)
    {   
    	List<PerfCheckreport> resultlist = new ArrayList<PerfCheckreport>();
    	List<PerfCheckreport> perfCheckreports = perfCheckreportMapper.selectYearList(perfCheckreport);
    	for (PerfCheckreport p: perfCheckreports)
    	{   //生成数组，循环12次，变量 i + 1 每次与月份比较，判断是否存在月份，存在则去绩效，不存在则补0
    		List<String> checkmonths = Arrays.asList( p.getCheckmonths().split(","));
    		String levels = p.getLastlevel();
    		if (StringUtils.isBlank(levels) || levels == null)
    		{
    			levels = "-,-,-,-,-,-,-,-,-,-,-,-";
    		}
			 String[] lastlevel = levels.split(","); 
        	 StringBuffer s = new StringBuffer();
        	 int j = 0;
        	 for (int i = 0;i < 12; i++)
        	 {  
        		 //判断i + 1是否存在列表中，存在返回大于0，不存在返回小于0
        		 String months = Integer.toString(i + 1);
        		if ( checkmonths.contains(months) )
        		{   
        			String level = lastlevel[j];
        			if (StringUtils.isNotBlank(level) && level != null)
        			{
        				s.append( level + ",");
        			}
        			else {
        				s.append("-,");
					}
        			j++;
        		}
        		else {
        			s.append("-,");
				}
        	 }
        	 //StringBuffer 拼接字符串时，删除最后一个逗号
        	 String lastlevels = s.deleteCharAt(s.length() - 1).toString();
        	 p.setLastlevel(lastlevels);
        	 resultlist.add(p);
    	}
         return resultlist;
    }
    
    /**
     * 查询绩效报告单列表
     * 
     * @param perfCheckreport 绩效报告单
     * @return 绩效报告单
     */
    @Override
    public List<PerfCheckreport> selectPerfCheckreportList(PerfCheckreport perfCheckreport)
    {   
		// 拆分状态字符串
		List<String> statuslist = perfCheckreport.getStatuslist();
		if (statuslist != null && statuslist.size() > 0) {
			String status = "'" + StringUtils.join(statuslist, "','") + "'";
			perfCheckreport.setStatus(status);
		}
    	
        return perfCheckreportMapper.selectPerfCheckreportList(perfCheckreport);
    }


    /**
     * 新增绩效报告单
     * 
     * @param perfCheckreport 绩效报告单
     * @return 结果
     */
    @Override
    public int insertPerfCheckreport(PerfCheckreport perfCheckreport)
    {
        return perfCheckreportMapper.insertPerfCheckreport(perfCheckreport);
    }

    /**
     * 修改绩效报告单
     * 
     * @param perfCheckreport 绩效报告单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updatePerfCheckreport(Map<String,List<PerfCheckreport>>  datas)
    {   

		String result = "1";
		List<String> check = new ArrayList<String>();
    	HttpServletRequest request = ServletUtils.getRequest();
		String username = request.getHeader(Constants.CURRENT_USERNAME);
		String datedate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		for (PerfCheckreport perfCheckreport : datas.get("datas"))
		{
			perfCheckreport.setLastdate(datedate);
			perfCheckreport.setLastusername(username);
			String lastlevel = perfCheckServiceImpl.getlevelResult(perfCheckreport.getLastscore(),perfCheckreport.getCheckcode());
			if (lastlevel.contains("管理员"))
			{
				return lastlevel;
			}
			perfCheckreport.setLastlevel(lastlevel);
			//此处需要补充代码：当reportstatus不为空时，更改考核报告状态
		    try {
				perfCheckreportMapper.updatePerfCheckreport(perfCheckreport);
			} catch (Exception e) {
				result = "修改考核单" + perfCheckreport.getCheckcode() + "失败,请联系管理员！";
				log.error(result, e);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return result;
			}
		}
		if (check.size() > 0 && check != null) {
			result = Joiner.on(",").join(check);
		}
		return result;
    }

    /**
     * 删除绩效报告单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePerfCheckreportByIds(String ids)
    {
        return perfCheckreportMapper.deletePerfCheckreportByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除绩效报告单信息
     * 
     * @param perfcheckreportId 绩效报告单ID
     * @return 结果
     */
    public int deletePerfCheckreportById(Long perfcheckreportId)
    {
        return perfCheckreportMapper.deletePerfCheckreportById(perfcheckreportId);
    }
}
